package github.leavesczy.reactivehttpsamples.util

import android.text.TextUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.PathUtils
import github.leavesczy.reactivehttpsamples.core.mode.SensorLocation
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import java.io.FileWriter


object FileUtilHelper {

    const val MULTIPART_FORM_DATA: String = "multipart/form-data"
    const val PATH_ROOT:String="/Bridge"
    const val PATH_FILE:String="${PATH_ROOT}/Files"

    fun putRequestBodyMap(map: MutableMap<String, RequestBody>, key: String?, value: String?) {
        putRequestBodyMap(map, key, createPartFromString(value))
    }

    fun createPartFromString(descriptionString: String?): RequestBody {
        var descriptionString = descriptionString
        if (descriptionString == null) {
            descriptionString = ""
        }
        return RequestBody.create(
            MediaType.parse(MULTIPART_FORM_DATA), descriptionString
        )
    }

    fun putRequestBodyMap(map: MutableMap<String, RequestBody>, key: String?, body: RequestBody?) {
        if (!TextUtils.isEmpty(key) && body != null) {
            map[key!!] = body
        }
    }

    fun prepareFilePart(partName: String?, fileUri: String?): MultipartBody.Part? {
        val file = File(fileUri)
        if (file != null) {
            // 为file建立RequestBody实例
            val requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file)
            // MultipartBody.Part借助文件名完成最终的上传
            return MultipartBody.Part.createFormData(partName, file.name, requestFile)
        }
        return null
    }
    fun prepareFilePart(partName: String, file: File): MultipartBody.Part {
            // 为file建立RequestBody实例
            val requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file)
            // MultipartBody.Part借助文件名完成最终的上传
            return MultipartBody.Part.createFormData(partName, file.name, requestFile)
    }
    //应用内部存储路径 不需要存储权限
    fun createCsvFile(bridgeCode:String):File?{
        val externalAppFilesPath = PathUtils.getExternalAppFilesPath()
        val path=externalAppFilesPath+"/csv/${bridgeCode}/${System.currentTimeMillis()}.csv"
        Logger.d("createCsvFile,path:"+path)
        val  file=FileUtils.getFileByPath(path)
        val createOrExistsFile = FileUtils.createOrExistsFile(file)
        if(!createOrExistsFile){
            return null
        }
        return file
    }

    //应用外部存储路径 需要存储权限
    fun createCsvFileStoragePath(bridgeCode:String):File?{
        val storagePath = PathUtils.getExternalStoragePath()
        val path=storagePath+"${PATH_FILE}/csv/${bridgeCode}/${System.currentTimeMillis()}.csv"
        Logger.d("createCsvFileStoragePath,path:"+path)
        val  file=FileUtils.getFileByPath(path)
        val createOrExistsFile = FileUtils.createOrExistsFile(file)
        if(!createOrExistsFile){
            return null
        }
        return file
    }

    fun writerCsv(currentFile: File, mDataList: MutableList<SensorLocation>) {
        val fileWriter=FileWriter(currentFile,true)
        fileWriter.append(formatCsv("时间","x信号","y信号","z信号","纬度","经度"))
        mDataList.forEach { item->
            fileWriter.append(formatCsv(item.time,item.xAxis,item.yAxis,item.zAxis,item.latitude,item.longitude))
        }
        //刷新缓冲区
        fileWriter.flush()
        //关闭写入器
        fileWriter.close()
    }

    private fun formatCsv(firstRow: Any,secondRow: Any,thirdRow: Any,fourRow: Any,fiveRow: Any,sixRow: Any):String{
        return "${firstRow},${secondRow},${thirdRow},${fourRow},${fiveRow},${sixRow}\n"
    }


}